package com.huahua.graduateschoolspringboot.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.huahua.graduateschoolspringboot.entity.param.LightChangeStatusParam;
import com.huahua.graduateschoolspringboot.entity.po.CityLightConfig;
import com.huahua.graduateschoolspringboot.enums.DeviceTypeEnum;
import com.huahua.graduateschoolspringboot.mapper.CityLightConfigMapper;
import com.huahua.graduateschoolspringboot.service.CityLightConfigService;
import com.huahua.graduateschoolspringboot.util.MessageUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author LoveHuaHua
 * @date 2023年05月06日 13:15
 * @description believe in yourself
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Lazy))
public class CityLightConfigServiceImpl extends ServiceImpl<CityLightConfigMapper, CityLightConfig> implements CityLightConfigService {

    private final ObjectMapper objectMapper;


    /**
     * 改变状态
     *
     * @param lightCustomDTO 光自定义dto
     */
    @Override
    public void updateMode(LightChangeStatusParam lightCustomDTO) throws JsonProcessingException {
        CityLightConfig lightConfig = getById(lightCustomDTO.getId());
        lightConfig.setStatus(lightCustomDTO.getStatus());
        lightConfig.setStartTime(lightCustomDTO.getStartTime());
        lightConfig.setEndTime(lightCustomDTO.getEndTime());
        if (CollUtil.isNotEmpty(lightCustomDTO.getDeviceListArr())){
            lightConfig.setDeviceList(StrUtil.join(",", lightCustomDTO.getDeviceListArr()));
        }
        updateById(lightConfig);
        String json = lightConfig.getJson();
        ObjectNode jsonNode = objectMapper.readValue(json, ObjectNode.class);
        //设置状态
        jsonNode.put("status", lightCustomDTO.getStatus());
        if (lightCustomDTO.getStartTime() != null && lightCustomDTO.getEndTime() != null) {
            ObjectNode objectNode = objectMapper.createObjectNode();
            ArrayNode endTime = objectMapper.createArrayNode();
            endTime.add(lightCustomDTO.getEndTime().getHour());
            endTime.add(lightCustomDTO.getEndTime().getMinute());
            endTime.add(lightCustomDTO.getEndTime().getSecond());
            objectNode.set("end", endTime);


            ArrayNode startTime = objectMapper.createArrayNode();
            startTime.add(lightCustomDTO.getStartTime().getHour());
            startTime.add(lightCustomDTO.getStartTime().getMinute());
            startTime.add(lightCustomDTO.getStartTime().getSecond());
            objectNode.set("start", startTime);

            jsonNode.set("time", objectNode);
        }

        if (lightCustomDTO.getDeviceListArr() != null) {
            ObjectNode objectNode = objectMapper.createObjectNode();
            objectNode.put("Brightness", 80);
            ArrayNode arrayNode = objectMapper.createArrayNode();
            List<Integer> collect = Stream.iterate(1, n -> n + 1)
                    .limit(18)
                    .collect(Collectors.toList());
            for (Integer integer : collect) {
                if (lightCustomDTO.getDeviceListArr().contains(integer)) {
                    arrayNode.add(1);
                } else {
                    arrayNode.add(0);
                }
            }

            objectNode.set("status", arrayNode);

            jsonNode.set("LampStatus", objectNode);

        }

        MessageUtil.sendMessage(JSONUtil.toJsonPrettyStr(JSONUtil.parseObj(objectMapper.writeValueAsString(jsonNode))), DeviceTypeEnum.LIGHT, 2);

    }
}
